<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>

<body>
    <div id="app">
        <h2>ref的使用 如果是一个组件,使用$refs获取到的组件的实例引用</2>
            <item @fasong="jieshou" ref="one"></item>
            <item @fasong="jieshou" ref="two"></item>
            <div>我现在要求上面2个组件的总和:{{total}}</div>
    </div>
    <script>
        Vue.component("item", {
            data() {
                return {
                    num: 0
                }
            },
            template: "<div @click='add'>{{num}}</div>",
            methods: {
                add() {
                    this.num++,
                        this.$emit("fasong")
                }
            },

        })

        new Vue({
            el: "#app",
            data: {

            },
            methods: {
                jieshou() {
                    this.total = this.$refs.one.num + this.$refs.two.num
                }
            },
        })
    </script>
</body>

</html>